package whr.modules.assets.controller;

import java.util.Arrays;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import whr.modules.assets.entity.ReceiveEntity;
import whr.modules.assets.service.ReceiveService;
import whr.common.utils.PageUtils;
import whr.common.utils.R;



/**
 * 资产领用表
 *
 * @author liuli
 * @email 2547121830.com
 * @date 2024-09-23 11:36:58
 */
@RestController
@RequestMapping("assets/receive")
@Slf4j
public class ReceiveController {
    @Autowired
    private ReceiveService receiveService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("assets:receive:list")
    public R list(@RequestParam Map<String, Object> params){
        log.info("parama{}",params);
        PageUtils pageUtils =receiveService.queryByRole(params);
        return R.ok().put("page", pageUtils);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{receiveId}")
    @RequiresPermissions("assets:receive:info")
    public R info(@PathVariable("receiveId") Long receiveId){
		ReceiveEntity receive = receiveService.getById(receiveId);

        return R.ok().put("receive", receive);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("assets:receive:save")
    public R save(@RequestBody ReceiveEntity receive){
		receiveService.save(receive);

        return R.ok();
    }
    /**
     * 资产领用申请
     */
    //
    @RequestMapping("/applySave")
    @RequiresPermissions("assets:receive:applySave")
    public R applyInfo(@RequestBody ReceiveEntity receive){
       // log.info("reveice{}",receive);
        try{
            receiveService.applySave(receive);
        }catch (Exception e){
            return R.error(e.getMessage());
        }

        return R.ok("申请成功");
    }
    /***
     * 审核(所有审核（领用/报修）)
     *
     */
    @RequestMapping("/autitorSave")
    @RequiresPermissions("assets:receive:autitor")
    public R autitorSave(@RequestBody ReceiveEntity receive){
        log.info("receive{}",receive);
        try{
            receiveService.updateByStatus(receive);
        }catch (Exception e){
            return R.error(e.getMessage());
        }
        return R.ok("审核成功");
    }

    /**
     * 领用归还
     * @param receive
     * @return
     */
    @RequestMapping("/assetsReturn")
    @RequiresPermissions("assets:receive:return")
    public R assetsReturn(@RequestBody ReceiveEntity receive){
        log.info("receive{}",receive);
        try{
            receiveService.assetsReturn(receive);
        }catch (Exception e){
            return R.error(e.getMessage());
        }
        return R.ok("审核成功");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("assets:receive:update")
    public R update(@RequestBody ReceiveEntity receive){
       /* // 通过审核如果同意归还，数量+1
        receiveService.updateByCreateStatus(receive);*/
		receiveService.updateById(receive);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("assets:receive:delete")
    public R delete(@RequestBody Long[] receiveIds){
		receiveService.removeByIds(Arrays.asList(receiveIds));

        return R.ok();
    }

}
